myApp.controller('stock', ['$scope', '$http', function($scope, $http){
	$scope.point = 'search';

	$scope.allStorage = [];
	$http.get('searchAllStorage').then(function successCallback(response) {
		$scope.allStorage = response.data;
	}, function errorCallback(response) {
		console.log("fail：" + response);
	});

}]);


myApp.controller('searchStock', ['$scope', '$http', '$shop', function($scope, $http, $shop){
	
	$scope.barcode = "";
	$scope.storageId1 = "";
	$scope.storageId2 = "";
	$scope.allStock = [];

	$scope.searchStockByInfo = function(type) {

		if($scope.barcode === "") {
			alert("请填写条形码！");
			return;
		}

		$scope.allStock = [];

		var postCfg = {
			headers: { 'Content-Type': 'application/x-www-form-urlencoded' },
			transformRequest: function (data) {
				return $.param(data);
			}
		};

		var stockData;
		if(type === 0) {
			stockData = {
				"stock.stockId": $shop.myShop.shopId,
				"stock.type": 0,
				"stock.barcode": $scope.barcode
			};
		}else {
			if($scope.storageId1 === "" || $scope.storageId1 === null) {
				alert("请选择仓库！");
				return;
			}

			stockData = {
				"stock.stockId": $scope.storageId1,
				"stock.type": 1,
				"stock.barcode": $scope.barcode
			};			
		}	

		$http.post('searchStockByInfo', stockData, postCfg).then(function successCallback(response) {
			if(response.data[0].Nothing === "Nothing") {
				alert("查询不到！");
			}else {
				$scope.allStock = response.data;
			}				
			$scope.barcode = "";
			$scope.storageId1 = "";
			$scope.storageId2 = "";
			console.log("ok：" + response);
		}, function errorCallback(response) {
			alert("查询操作失败!");
			console.log("fail：" + response);
		});			

	};
	
	$scope.searchAllStock = function(type) {

		$scope.allStock = [];

		var postCfg = {
			headers: { 'Content-Type': 'application/x-www-form-urlencoded' },
			transformRequest: function (data) {
				return $.param(data);
			}
		};

		var stockData;
		if(type === 0) {
			stockData = {
				"stock.stockId": $shop.myShop.shopId,
				"stock.type": 0
			};			
		}else {
			if($scope.storageId2 === "" || $scope.storageId2 === null) {
				alert("请选择仓库！");
				return;
			}

			stockData = {
				"stock.stockId": $scope.storageId2,
				"stock.type": 1
			};			
		}	

		$http.post('searchAllStock', stockData, postCfg).then(function successCallback(response) {
			if(response.data[0].Nothing === "Nothing") {
				alert("查询不到！");
			}else {
				$scope.allStock = response.data;
			}				
			$scope.barcode = "";
			$scope.storageId1 = "";
			$scope.storageId2 = "";
			console.log("ok：" + response);
		}, function errorCallback(response) {
			alert("查询操作失败!");
			console.log("fail：" + response);
		});

	};

}]);


myApp.controller('modifyStock', ['$scope', '$http', '$shop', function($scope, $http, $shop){

	$scope.barcode = "";
	$scope.shopQuantity = "";
	$scope.updateShopQuantity = "";
	$scope.storageId = "";
	$scope.storageQuantity = "";
	$scope.updateStorageQuantity = "";
	var stock = [];

	$scope.searchStockByInfo = function(type) {

		if($scope.barcode === "") {
			alert("请填写条形码！");
			return;
		}

		stock = [];

		var postCfg = {
			headers: { 'Content-Type': 'application/x-www-form-urlencoded' },
			transformRequest: function (data) {
				return $.param(data);
			}
		};

		var stockData;
		if(type === 0) {
			stockData = {
				"stock.stockId": $shop.myShop.shopId,
				"stock.type": 0,
				"stock.barcode": $scope.barcode
			};
		}else {
			if($scope.storageId === "" || $scope.storageId === null) {
				alert("请选择仓库！");
				return;
			}

			stockData = {
				"stock.stockId": $scope.storageId,
				"stock.type": 1,
				"stock.barcode": $scope.barcode
			};			
		}	

		$http.post('searchStockByInfo', stockData, postCfg).then(function successCallback(response) {
			if(response.data[0].Nothing === "Nothing") {
				alert("查询不到！");
			}else {
				stock = response.data;
				if (type === 0) {
					$scope.shopQuantity = stock[0].quantity;
				}else {
					$scope.storageQuantity = stock[0].quantity;
				}
			}				
			console.log("ok：" + response);
		}, function errorCallback(response) {
			alert("查询操作失败!");
			console.log("fail：" + response);
		});	

	};

	$scope.updateShopStock = function() {

		if($scope.barcode === "") {
			alert("请填写条形码！");
			return;
		}
		if($scope.updateShopQuantity === "") {
			alert("请填写更新数量！");
			return;
		}

		var stockData = {
			"stock.stockId": $shop.myShop.shopId,
			"stock.type": 0,
			"stock.barcode": $scope.barcode,
			"stock.quantity": $scope.updateShopQuantity
		};

		var postCfg = {
			headers: { 'Content-Type': 'application/x-www-form-urlencoded' },
			transformRequest: function (data) {
				return $.param(data);
			}
		};

		$http.post('modifyStock', stockData, postCfg).then(function successCallback(response) {
			alert("已成功更新库存！");			
			console.log("ok：" + response);
		}, function errorCallback(response) {
			alert("更新操作失败！");
			console.log("fail：" + response);
		});	

	};

	$scope.updateStorageStock = function() {

		if($scope.barcode === "") {
			alert("请填写条形码！");
			return;
		}
		if($scope.storageId === "" || $scope.storageId === null) {
			alert("请选择仓库！");
			return;
		}
		if($scope.updateStorageQuantity === "") {
			alert("请填写更新数量！");
			return;
		}

		var stockData = {
			"stock.stockId": $scope.storageId,
			"stock.type": 1,
			"stock.barcode": $scope.barcode,
			"stock.quantity": $scope.updateStorageQuantity
		};

		var postCfg = {
			headers: { 'Content-Type': 'application/x-www-form-urlencoded' },
			transformRequest: function (data) {
				return $.param(data);
			}
		};

		$http.post('modifyStock', stockData, postCfg).then(function successCallback(response) {
			alert("已成功更新库存！");			
			console.log("ok：" + response);
		}, function errorCallback(response) {
			alert("更新操作失败！");
			console.log("fail：" + response);
		});	

	};

	$scope.cancle = function(type) {

		$scope.barcode = "";
		stock = [];
		if(type === 0) {
			$scope.shopQuantity = "";
			$scope.updateShopQuantity = "";
		}else {
			$scope.storageId = "";
			$scope.storageQuantity = "";
			$scope.updateStorageQuantity = "";
		}

	}

}]);